
App.register("ContributionsReceiptSettings", ContributionsReceiptSettings = {
    
    contributions_id: null,
    
    init: function() {
        var self = this;

        $("#save_receipt_settings").click(function(){
            self.save();
        });
    },

    animate_save:function(animate){
        var self = this;
        if(typeof animate == 'undefined'){
            animate = true;
        }

        if(animate){
            $('#receipt_mode').attr('disabled','disabled');
            $('#receipt_prefix').attr('disabled','disabled');
            $('#receipt_postfix').attr('disabled','disabled');
            $('#receipt_counter').attr('disabled','disabled');
            $('#receipt_settings_actions_btn').hide();
            $('#receipt_settings_loader').show();
        }
        else{
            $('#receipt_mode').removeAttr('disabled');
            $('#receipt_prefix').removeAttr('disabled');
            $('#receipt_postfix').removeAttr('disabled');
            $('#receipt_counter').removeAttr('disabled');
            $('#receipt_settings_actions_btn').show();
            $('#receipt_settings_loader').hide();
        }
    },

    save: function() {
        var self = this;

        var url = $("#receipt-settings-form").attr('action');
        var receipt_mode = $("#receipt_mode").val();
        var receipt_prefix = $("#receipt_prefix").val();
        var receipt_postfix = $("#receipt_postfix").val();
        var receipt_counter = $("#receipt_counter").val();
        var params = "receipt_mode="+receipt_mode+"&receipt_prefix="+receipt_prefix+"&receipt_postfix="+receipt_postfix+"&receipt_counter="+receipt_counter;

        self.animate_save(true);

        $.ajax({
            url: url,
            data: params,
            type: 'post',
            dataType: 'json',
            success: function(res) {
                self.animate_save(false);
                if( typeof res['status'] != "undefined" ) {
                    if( res['status'] == 'success' ) {
                        alertify.success(res['message']);
                    }
                    else {
                        alertify.error(res['message']);
                    }
                }
                else {
                    alert(JSON.stringify(res));
                }
            },
            error: function() {
                self.animate_save(false);
                alertify.error("Failed to save receipt settings.");
            }
        });
    },

    post_save:function(res){
        var self = this;

        var data = res.data;
        $("#contributions-table tbody tr").removeClass("selected");
        var tr = $('<tr />').addClass("selected");
        var td = $('<td />');
        var checkbox = $('<input />').attr('type','checkbox').val(data.id);
        td.append(checkbox);
        tr.append(td);
        var td = $('<td />').addClass('info').attr('data-id',data.id).html(data.name);
        tr.append(td);
        $('#contributions-table tbody').append(tr);
        App.notify('contributions_types.load', data.id);

    }
});

